﻿
function htmlMarkRowAsUnread(divMarkerId) {
    var divTag = document.getElementById(divMarkerId);
    if (divTag) {
        //Since we stole the direction attribute, the XSL
        //wraps us in a span, once thats fixed remove the 
        //extra parentnode here
        var divRow = divTag.parentNode.parentNode.parentNode;
        var rowColor = ru_UnreadColor;
        if (rowColor != "") {
            setColorOnTagAndAllChildTags(divRow, rowColor);
        }
        else {
            setFontOnTagAndAllChildTags(divRow, "bolder");
        }
    }
}

function htmlMarkRowAsRead(divMarkerId) {
    var divTag = document.getElementById(divMarkerId);
    if (divTag) {
        //Since we stole the direction attribute, the XSL
        //wraps us in a span, once thats fixed remove the 
        //extra parentnode here
        var divRow = divTag.parentNode.parentNode.parentNode;
        var rowColor = ru_UnreadColor;
        if (rowColor != "") {
            removeColorOnTagAndAllChildTags(divRow);
        }
        else {
            setFontOnTagAndAllChildTags(divRow, "normal");
        }
    }
}

function imageMarkRowAsUnread(divMarkerId) {
    var imgId = "readUnreadImage_" + divMarkerId.replace("readMarkerRow", "");
    var imgTag = document.getElementById(imgId);
    if (imgTag) {
        imgTag.src = ru_UnreadImageUrl;
    }
}

function imageMarkRowAsRead(divMarkerId) {
    var imgId = "readUnreadImage_" + divMarkerId.replace("readMarkerRow", "");
    var imgTag = document.getElementById(imgId);
    if (imgTag) {
        imgTag.src = ru_ReadImageUrl;
    }
}

function markRowAsUnread(divMarkerId) {

    if (ru_ColumnRenderMode == "BoldDisplay") {
        htmlMarkRowAsUnread(divMarkerId);
    }
    else {
        imageMarkRowAsUnread(divMarkerId);
    }
}

function markRowAsRead(divMarkerId) {

    if (ru_ColumnRenderMode == "BoldDisplay") {
        htmlMarkRowAsRead(divMarkerId);
    }
    else {
        imageMarkRowAsRead(divMarkerId);
    }
}

function setFontOnTagAndAllChildTags(parentTag, fontValue) {
    try {
        parentTag.style.fontWeight = fontValue;
    }
    catch (e) { }
    var childCount = parentTag.childNodes.length;
    for (var i = 0; i < childCount; i++) {
        setFontOnTagAndAllChildTags(parentTag.childNodes[i], fontValue);
    }
}

function setColorOnTagAndAllChildTags(parentTag, colorValue) {
    try {
        parentTag.style.color = colorValue; 
        parentTag.style.fontWeight = "bolder";
    }
    catch (e) { }
    var childCount = parentTag.childNodes.length;
    for (var i = 0; i < childCount; i++) {
        setColorOnTagAndAllChildTags(parentTag.childNodes[i], colorValue);
    }
}

function removeColorOnTagAndAllChildTags(parentTag) {
    try {
        parentTag.style.color = "";
        parentTag.style.fontWeight = "normal";
    }
    catch (e) { }
    var childCount = parentTag.childNodes.length;
    for (var i = 0; i < childCount; i++) {
        removeColorOnTagAndAllChildTags(parentTag.childNodes[i]);
    }
}

function MarkItemReadOrUnread(readMark, itemID, divMarkerId) {

    var requestURL = ctx.HttpRoot + "/_Layouts/Voodoo.ListItemReadUnreadSiteColumn/LazyReadUnReadUpdate.aspx";
    requestURL += "?iID=" + itemID;
    requestURL += "&lID=" + ctx.listName;
    requestURL += "&nValue=" + readMark;
    requestURL += "&pName=" + ru_FieldName;
    var httpRequest = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
    httpRequest.onreadystatechange = function () {
        if (4 == httpRequest.readyState) {
            if ("read" == httpRequest.responseText) {
                markRowAsRead(divMarkerId);
            }
            else {
                if ("unread" == httpRequest.responseText) {
                    markRowAsUnread(divMarkerId);
                }
                //TODO should we mark the row in error ?
            }
        }
    };
    //window.open(requestURL);
    httpRequest.open("GET", requestURL, true);
    httpRequest.send(null);
}